package com.agileai.portal.controller.wcm;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONObject;

import com.agileai.domain.DataParam;
import com.agileai.domain.DataRow;
import com.agileai.hotweb.controller.core.SimpleHandler;
import com.agileai.hotweb.domain.core.Profile;
import com.agileai.hotweb.domain.core.Resource;
import com.agileai.hotweb.domain.core.User;
import com.agileai.hotweb.renders.LocalRenderer;
import com.agileai.hotweb.renders.ViewRenderer;
import com.agileai.portal.bizmoduler.wcm.InfomationManage;
import com.agileai.util.DateUtil;
import com.agileai.util.StringUtil;

/**
 * generated by miscdp
 */
public class ContentProviderHandler extends SimpleHandler{
	public ContentProviderHandler(){
		super();
	}
	
	public ViewRenderer prepareDisplay(DataParam param) {
		String contentId = param.get("contentId");
		if (StringUtil.isNotNullNotEmpty(contentId)){
			InfomationManage infomationManage = this.lookupService(InfomationManage.class);
			String html = infomationManage.getInfomationContent(contentId);
			if (html != null){
				if (html.startsWith("<script>") && html.endsWith("</script>")){
					html = html.substring(8,html.length()-9);
				}
				else if (html.startsWith("<pre>") && html.endsWith("</pre>")){
					html = html.substring(5,html.length()-6);
				}
				this.setAttribute("content",html);
			}
		}
		else {
			String columnId =  param.get("columnId");
			String maxCountValue = param.get("maxCount");
			String sortPolicy = param.get("sortPolicy");
			
			JSONObject jsonObject = new JSONObject();
			int count = 0;
			List<DataRow> records = null;
			InfomationManage infomationManage = this.lookupService(InfomationManage.class);
			if (!StringUtil.isNullOrEmpty(maxCountValue) && !StringUtil.isNullOrEmpty(sortPolicy)){
				int maxCount = Integer.parseInt(maxCountValue);
				List<DataRow> tempRecords = infomationManage.findContentRecords(columnId,sortPolicy,InfomationManage.InfoState.Published);
				records = this.getAuthedRecords(tempRecords);
				count = (maxCount > records.size()) ? records.size():maxCount;
			}else{
				List<DataRow> tempRecords = infomationManage.findContentRecords(columnId,sortPolicy,InfomationManage.InfoState.Published);
				records = this.getAuthedRecords(tempRecords);
				count = records.size();
			}
			try {
				if (count > 0){
					JSONArray jsonArray = new JSONArray();
					jsonObject.put("infomations", jsonArray);
					for (int i=0;i < count;i++){
						DataRow row = records.get(i);
						if (StringUtil.isNotNullNotEmpty(columnId) &&  i == 0){
							String ptTitle = row.stringValue("PT_TITLE");
							String columnName = row.stringValue("COL_NAME");
							jsonObject.put("columnId", columnId);
							jsonObject.put("columnName", columnName);
							jsonObject.put("columnTitle", ptTitle);
						}
						JSONObject tempJsonObject = new JSONObject();
						String infomationId = row.stringValue("INFO_ID");
						String infomationTitle = row.stringValue("INFO_TITLE");
						String infomationShortTitle = row.stringValue("INFO_SHORTTITLE");
						String readCount = row.stringValue("INFO_READ_COUNT");
						Date publishTime = (Date)row.get("INFO_PUBLISH_TIME");
						jsonArray.put(tempJsonObject);
						tempJsonObject.put("id", infomationId);
						tempJsonObject.put("title", infomationTitle);
						tempJsonObject.put("shortTitle", infomationShortTitle);
						tempJsonObject.put("readCount", readCount);
						tempJsonObject.put("publishTime", DateUtil.getDateByType(DateUtil.YYMMDDHHMI_SLANTING,publishTime));
					}
				}
				this.setAttribute("content",jsonObject.toString());
			} catch (Exception e) {
				this.log.error(e.getLocalizedMessage(), e);
			}
		}
		return new LocalRenderer(getPage());
	}
	
	private List<DataRow> getAuthedRecords(List<DataRow> tempRecords){
		List<DataRow> result = new ArrayList<DataRow>();
		Profile profile = this.getProfile();
		if (profile != null){
			User user = (User)profile.getUser();
			for (int i=0;i < tempRecords.size();i++){
				DataRow row = tempRecords.get(i);
				String authType = row.stringValue("INFO_AUTH_TYPE");
				if (InfomationManage.AuthType.Private.equals(authType)){
					String infomatonId = row.stringValue("INFO_ID");
					if (user.containResouce(Resource.Type.Infomation, infomatonId)
							|| "user".equals(user.getUserCode()) || "admin".equals(user.getUserCode())){
						result.add(row);
					}
				}else{
					result.add(row);
				}
			}
		}else{
			for (int i=0;i < tempRecords.size();i++){
				DataRow row = tempRecords.get(i);
				String authType = row.stringValue("INFO_AUTH_TYPE");
				if (InfomationManage.AuthType.Public.equals(authType)){
					result.add(row);
				}
			}
		}
		return result;
	}
}
